package com.hundsun.cprs.yyt.service.match.core;

import java.util.concurrent.Executor;

import com.hundsun.cprs.yyt.service.match.core.command.CancelEntrustCommand;
import com.hundsun.cprs.yyt.service.match.core.command.DefaultMatchCommand;
import com.hundsun.cprs.yyt.service.match.core.command.RollbackEntrustCommand;
import com.hundsun.cprs.yyt.service.match.core.command.AmountSplitBaseCommand;
import com.hundsun.cprs.yyt.service.match.domain.TradeEntrust;
import com.hundsun.cprs.yyt.service.match.dto.EntrustRequest;
import com.hundsun.cprs.yyt.service.trade.domain.TradeCancelFlow;
import com.hundsun.cprs.yyt.service.trade.domain.TradeRollbackFlow;

public class TradeCenter {

    private static final int DEFAULT_MATCH_BUY_NEWUSER_RATE = 50;

    private String stockCode;

    private Matching matching;
    
    private int matchBuyNewUserRate = DEFAULT_MATCH_BUY_NEWUSER_RATE;

    public TradeCenter(String stockCode,Executor executor) {
        this.stockCode = stockCode;
        this.matching = new Matching(this,executor);
    }
    
    public void addEntrust(EntrustRequest entrustRequest){
        TradeEntrust entrust = new TradeEntrust(entrustRequest);
        this.matching.putCommand(new AmountSplitBaseCommand(entrust));
    }
    
    public void addEntrust(TradeEntrust tradeEntrust){
        this.matching.putCommand(new AmountSplitBaseCommand(tradeEntrust));
    }
    
    public void rollbackEntrust(TradeEntrust tradeEntrust,TradeRollbackFlow applyRollback){
        this.matching.putCommand(new RollbackEntrustCommand(applyRollback,tradeEntrust));
    }

    public void cancelEntrust(TradeEntrust tradeEntrust,TradeCancelFlow cancelFlow){
        this.matching.putCommand(new CancelEntrustCommand(cancelFlow,tradeEntrust));
    }
    /**
     * 释放资料
     */
    public void release() {
        this.shutdown();
        this.matching = null;
    }
    
    /**
     * 开始撮合处理
     */
    public void start() {
        this.matching.start();
    }
    
    /**
     * 关闭撮合处理
     */
    public void shutdown() {
        this.matching.shutdown();
    }

    public String getStockCode() {
        return stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public Matching getMatching() {
        return matching;
    }

    public int getMatchBuyNewUserRate() {
        return matchBuyNewUserRate;
    }

    public void setMatchBuyNewUserRate(int matchBuyNewUserRate) {
        if (matchBuyNewUserRate > 100 || matchBuyNewUserRate < 0) {
            //设置无效,按上次设置为准
            return;
        }
        this.matchBuyNewUserRate = matchBuyNewUserRate;
    }
}
